package com.leetcode.practice.self.ddd;

import com.leetcode.practice.self.entity.LogDomain;
import com.leetcode.practice.self.entity.RoleDomain;
import com.leetcode.practice.self.entity.UserDomain;
import com.leetcode.practice.self.listen.BaseEvent;
import com.leetcode.practice.self.listen.ListenerPublisher;
import com.leetcode.practice.self.listen.LogEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

@Service
@Slf4j
public class TestLogService {

    @Autowired
    private ListenerPublisher listenerPublisher;

    @EventListener
    public Long addTestRole(BaseEvent<RoleDomain> roleDomainBaseEvent) {
        log.info("添加角色");
        RoleDomain data = roleDomainBaseEvent.getData();
        LogDomain logDomain = new LogDomain(data.getUserId(),data.getName());
        LogEvent baseEvent = new LogEvent(UUID.randomUUID().toString(), LocalDateTime.now(),logDomain);
        listenerPublisher.pushBaseEvent(baseEvent);
        return logDomain.getUserId();
    }
}
